package com.general.template.controller;

import com.general.template.component.UploadService;
import com.general.template.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @Author: 杰哥
 * @Description:
 **/
@RestController
@RequestMapping("upload")
@Api(value="文件api",description = "上传文件api")
public class UploadController {

    @Autowired
    private UploadService uploadService;

    /**
     * 上传文件
     *
     * @param file
     * @return
     */
    @PostMapping("/uploadImage")
    @ApiOperation(value="上传文件接口",notes = "上传到Fasdfs服务器上")
    @ApiImplicitParam(name = "file",required = true, dataType = "File",paramType = "post")
    public Result<String> uploadImage(MultipartFile file) {
        String url = uploadService.uploadImage(file);
        return new Result<>("上传成功！", url);
    }

    @PostMapping("/uploadAdmin")
    @ApiOperation(value="上传图片接口",notes = "上传图片接口")
    @ApiImplicitParam(name = "file",required = true, dataType = "File",paramType = "post")
    public Result<String> uploadAdmin(MultipartFile file) {
        String url = uploadService.uploadAdmin(file);
        if (url==null){
            return new Result<>("上传失败！", "上传文件格式有误!");
        }else{
            return new Result<>("上传成功！", url);
        }
    }

    @PostMapping("/uploadMedia")
    @ApiOperation(value="上传媒体接口",notes = "上传媒体接口")
    @ApiImplicitParam(name = "file",required = true, dataType = "File",paramType = "post")
    public Result<Object> uploadMedia(MultipartFile[] file) {
        Map<String, Object> url = uploadService.uploadMedia(file);
        if (url==null){
            return new Result<>("上传失败！", "上传文件格式有误!");
        }else{
            return new Result<>("上传成功！", url);
        }
    }
}
